<template>
  <div class="login-container">
    <div class="login-box">
      <div class="headshot-box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form ref="loginFormRef" :model="form" class="login-form">
        <el-form-item>
          <el-input
            v-model="form.name"
            prefix-icon="el-icon-user"
            placeholder="用户名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            type="password"
            v-model="form.password"
            prefix-icon="el-icon-password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        name: "",
        password: "",
      },
    };
  },
  methods: {
    login() {
      this.$axios
        .post("/login", {
          username: this.form.name,
          password: this.form.password,
        })
        .then(response => {
          const { data: res } = response;
          console.log(res);
          this.$message.success('登录成功');
          window.sessionStorage.setItem("token", res.data.token);
          // 导航至/home
          this.$router.push("/home");
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    reset() {
      this.$refs.loginFormRef.resetFields();
    },
  },
};
</script>
<style lang="less" scoped>
.login-container {
  background: #2b4b6b;
  height: 100%;
  .login-box {
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .headshot-box {
      height: 130px;
      width: 130px;
      background-color: #808891;
      border: 1px solid #eee;
      border-radius: 50%;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .login-form {
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;
    }
    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
}
</style>
